﻿/*
  Copyright © Alexander G. Bykin, Russia 2009-2010
  This source is subject to the Microsoft Public License (Ms-PL).
  Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
  All other rights reserved.
*/

namespace System.Windows.Controls.UnitTests
{
    using System.Windows.Controls.Data;
    using System.Windows.Data;
    using Microsoft.Silverlight.Testing;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Collections.Generic;

    [TestClass]
    public class TestUtilities : SilverlightTest
    {
        #region Initialization

        /// <summary>
        /// Initializes the test framework.
        /// </summary>
        [TestInitialize]
        public void Initialize()
        {
        }

        #endregion Initialization

        #region CleanUp

        [TestCleanup]
        public void CleanUp()
        {
        }

        #endregion

        #region Test IsEntityFieldCanAdded

        #region Include Tests

        [TestMethod]
        [Asynchronous]
        public void Check_IsEntityFieldCanAdded_IncludeWorkingCorrect()
        {
            bool actual = true;

            var listOfFields = Utilities.ParseStringAsListOfString("test1, test2");

            this.EnqueueCallback(() =>
            {
                foreach (var item in listOfFields)
                {
                    int index = listOfFields.IndexOf(item);

                    if (index == 0)
                        Assert.AreEqual(Utilities.IsEntityFieldCanAdded(item, Utilities.ParseStringAsListOfString("test1"), Utilities.ParseStringAsListOfString("")), actual);
                    if (index == 1)
                        Assert.AreEqual(Utilities.IsEntityFieldCanAdded(item, Utilities.ParseStringAsListOfString("test2"), Utilities.ParseStringAsListOfString("")), actual);
                }
            });

            this.EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Check_IsEntityFieldCanAdded_IncludeWorkingCorrectWithCaseInsensitive()
        {
            bool actual = true;

            var listOfFields = Utilities.ParseStringAsListOfString("tEst1, tESt2");

            this.EnqueueCallback(() =>
            {
                foreach (var item in listOfFields)
                {
                    int index = listOfFields.IndexOf(item);

                    if (index == 0)
                        Assert.AreEqual(Utilities.IsEntityFieldCanAdded(item, Utilities.ParseStringAsListOfString("tesT1"), Utilities.ParseStringAsListOfString("")), actual);
                    if (index == 1)
                        Assert.AreEqual(Utilities.IsEntityFieldCanAdded(item, Utilities.ParseStringAsListOfString("TEst2"), Utilities.ParseStringAsListOfString("")), actual);
                }
            });

            this.EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Check_IsEntityFieldCanAdded_IncludeListHavePriorityUnderExcludeListWithRelatedEntities()
        {
            bool actual = true;

            var listOfFields = Utilities.ParseStringAsListOfString("test1.test1, test1.test2");

            this.EnqueueCallback(() =>
            {
                foreach (var item in listOfFields)
                {
                    Assert.AreEqual(Utilities.IsEntityFieldCanAdded(item, Utilities.ParseStringAsListOfString("test1.*"), Utilities.ParseStringAsListOfString("test1")), actual);
                }
            });

            this.EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Check_IsEntityFieldCanAdded_IncludeWorkingCorrectWithRelatedEntities()
        {
            bool actual = true;

            var listOfFields = Utilities.ParseStringAsListOfString("test1.test1, test1.test2");

            this.EnqueueCallback(() =>
            {
                foreach (var item in listOfFields)
                {
                    Assert.AreEqual(Utilities.IsEntityFieldCanAdded(item, Utilities.ParseStringAsListOfString("test1.*"), Utilities.ParseStringAsListOfString("")), actual);
                }
            });

            this.EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Check_IsEntityFieldCanAdded_IncludeListHavePriorityUnderExcludeList()
        {
            bool actual = true;

            var listOfFields = Utilities.ParseStringAsListOfString("test1, test2");

            this.EnqueueCallback(() =>
            {
                foreach (var item in listOfFields)
                {
                    Assert.AreEqual(Utilities.IsEntityFieldCanAdded(item, Utilities.ParseStringAsListOfString("test1, test2"), Utilities.ParseStringAsListOfString("test1, test2")), actual);
                }
            });

            this.EnqueueTestComplete();
        }

        #endregion

        #region Exclude Tests

        [TestMethod]
        [Asynchronous]
        public void Check_IsEntityFieldCanAdded_ExcludeWorkingCorrect()
        {
            bool actual = false;

            var listOfFields = Utilities.ParseStringAsListOfString("test1, test2");

            this.EnqueueCallback(() =>
            {
                foreach (var item in listOfFields)
                {
                    int index = listOfFields.IndexOf(item);

                    if (index == 0)
                        Assert.AreEqual(Utilities.IsEntityFieldCanAdded(item, Utilities.ParseStringAsListOfString(""), Utilities.ParseStringAsListOfString(item)), actual);
                }
            });

            this.EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Check_IsEntityFieldCanAdded_ExcludeWorkingCorrectWithCaseInsensitive()
        {
            bool actual = false;

            var listOfFields = Utilities.ParseStringAsListOfString("test1, test2");

            this.EnqueueCallback(() =>
            {
                foreach (var item in listOfFields)
                {
                    Assert.AreEqual(Utilities.IsEntityFieldCanAdded(item, Utilities.ParseStringAsListOfString(""), Utilities.ParseStringAsListOfString(item.ToUpper())), actual);
                }
            });

            this.EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Check_IsEntityFieldCanAdded_ExcludeWorkingCorrectWithRelatedEntities()
        {
            bool actual = false;

            var listOfFields = Utilities.ParseStringAsListOfString("test.test1, test.test2, test1");

            this.EnqueueCallback(() =>
            {
                foreach (var item in listOfFields)
                {
                    int index = listOfFields.IndexOf(item);
                    if (index < listOfFields.Count - 1)
                    {
                        Assert.AreEqual(Utilities.IsEntityFieldCanAdded(item, Utilities.ParseStringAsListOfString(""), Utilities.ParseStringAsListOfString("test.*")), actual);
                    }
                    else
                    {
                        Assert.AreEqual(Utilities.IsEntityFieldCanAdded(item, Utilities.ParseStringAsListOfString(""), Utilities.ParseStringAsListOfString("test.*")), true);
                    }
                }
            });

            this.EnqueueTestComplete();
        }

        #endregion

        #endregion

        #region Test ParseStringAsListOfString

        [TestMethod]
        [Asynchronous]
        public void Check_ParseStringAsListOfString_WorkingCorrect()
        {
            List<string> actual = new List<string>() { "test1", "test2" };
            this.EnqueueCallback(() =>
            {
                var expected = Utilities.ParseStringAsListOfString("tEst1, tESt2");
                foreach (var item in expected)
                {
                    int index = expected.IndexOf(item);
                    Assert.AreEqual(item, actual[index]);
                }
            });

            this.EnqueueTestComplete();
        }

        #endregion
    }
}
